/******************************************************************************
* HalfCOMM_Manager.h                                                                                                                                *
*                                                                                                                                          *
* Description : Half Send & Receive manager                                                                                                                   *
*                                                                                                                                           *
* Author : Henry  12/13/2012   :-)                                                                                                                  *
*                                                                                                                                         *
* History :                                                          
*
* for Master mode . 1:N mode.                                   
*                                                                                                                                       *
*                                                                                                                                      *
*                                                                                                                                          *
*******************************************************************************/
#ifndef __RS485MAN_
#define __RS485MAN__

#define GETSYSTM GetSysTime() //get sys tick
#define GET_ELAPSED(BeginTM) SystemGetElapsedTime(BeginTM) 

#define SUCC_POOL	1
#define FAIL_POOL	0

//error define as below
#define MAX_ERROR_TYPE 10  //one device ,maybe 10 error type most.
#define MAX_ERROR_POOL 32 //as mster ,maybe 32 device connect with it most.

//error TYPE
#define ERROR_TIMEOUT_	(2000+0) //time out. 
#define ERROR_TO_TRYOVER	(2000+1) //try times over.
#define ERROR_GET_SHORT	(2000+2) //get so short from slave
#define ERROR_POOL_OVER	(2000+3) //maybe ,pool over.

#define ERROR_NO			(2000+4) //success handle ^_^
#define ERROR_UNKNOW		(2000+5) //unknow
#define ERROR_CHECK		(2000+6) //check error.

#define EP_NULL	0
#define EP_BUSY	1
typedef struct error_Type__{
	unsigned int errorType;
	unsigned int Num; //error number.
}error_Type;

typedef struct error_Pool__ {
	unsigned int  DevType;
	unsigned int num;
	error_Type  Error[MAX_ERROR_TYPE];
	unsigned char state; // 0 invalid ,1 valid.
}error_Pool;



/*
	1 ,device type 
	2 ,device type & number. 
	3, device type & number & error type.
	4, device type & error type.
*/

#define ER_SC_DEVTP 			1
#define ER_SC_DEVTP_NUM 		2
#define ER_SC_DEVTP_NUM_ETP	3
#define ER_SC_DEVTP_ETP		4

//communication define as below .
#define COMM_BUF_LEN_MAX 		128 //  max pack size(BYTE) for 485 communication
#define COMM_LIST_LEN_MAX		64
#define DEF_TIMEOUT			400 // ms 
#define DEF_TRY_TIMES			3
#define ADD_AND_TO				5000 // add but do not send ,time out.

#define NODE_INVALID			0
#define NODE_ADD				1
#define NODE_WAIT				2 //wait receive.
#define NODE_WAIT_NEEDSEND	3 //try send.


#define  PRIORITY_1	1 // * 
#define  PRIORITY_2	2 // <  1
#define  PRIORITY_3	3
#define  PRIORITY_LOW_COMM	0xff

typedef void (* ErrorHandle)(struct commPoolMan__ * poolMan, unsigned int errorType);

typedef struct comm_pool_ {
	char dataBuffer[COMM_BUF_LEN_MAX];
	unsigned int bufferLen;
	unsigned int timeOutLimit;
	unsigned char State; //0, invalid, 1,add & wait Send, 2
	unsigned char sendPriority;
	unsigned int SendType; //send device type
	unsigned int SendNumber; //send device number. 	
	//unsigned char theListNullFlag; // 0 null, else have been added.
	unsigned int addNodeTime; //current time when add node .
	unsigned int sendTimes;//have been sent times
	//unsigned char theListValid; // 1, Valid ,wait for send, 0 invalid 
	ErrorHandle  errorHandle; //error handle function.
} comm_pool;

//pool manage define
#define POOL_FREE		0
#define POOL_WAIT		1
#define POOL_SEND		2
#define POOL_UNKNOW	3

/*
	return 0 ,succ,else error number.
*/
typedef unsigned char  (* sendFunction)(char * buffertoSend,DWORD RSBuflen) ;
typedef void (* revFunction)(unsigned int *errorNum);

typedef struct commPoolMan__ {
	unsigned char PoolDo;
	unsigned int currPos;  // point to pool current postion.	
	unsigned int validNodeNum; 
	comm_pool * COMMPool;
	error_Pool * ErrorPool;
	sendFunction  sendfunFromCOMM;
	revFunction revFunCOMM;
	unsigned int high32Send;
	unsigned int sendSuccCun;
	unsigned int high32Rev;
	unsigned int  revSuccCun;
}commPoolMan;


extern void poolInit_Half(commPoolMan * onePool,commPoolMan * OnePoolMan,error_Pool * oneErrorPool,
				sendFunction sendFun, revFunction revFun);
extern void poolRun(commPoolMan * poolMan);
extern unsigned char  addNodeToPool(commPoolMan * poolMan,char * sendBuf,unsigned int bufLen,
						unsigned char Priority, unsigned int Type,unsigned int number,unsigned int TO,
						ErrorHandle *errorHandle);

#endif